package com.shenyi.wx.service;

import com.shenyi.wx.api.IApiCouponService;
import com.shenyi.wx.dao.ApiCouponMapper;
import com.shenyi.wx.entity.CouponDO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


@Service
public class ApiCouponService implements IApiCouponService {
    @Autowired
    private ApiCouponMapper apiCouponMapper;

    public CouponDO queryObject(Integer couponId) {
        return apiCouponMapper.queryObject(couponId);
    }

    public List<CouponDO> queryList(Map<String, Object> map) {
        return apiCouponMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return apiCouponMapper.queryTotal(map);
    }


    public void save(CouponDO UserDO) {
        apiCouponMapper.save(UserDO);
    }

    public void update(CouponDO user) {
        apiCouponMapper.update(user);
    }

    public void delete(Long userId) {
        apiCouponMapper.delete(userId);
    }

    public void deleteBatch(Long[] userIds) {
        apiCouponMapper.deleteBatch(userIds);
    }

    public List<CouponDO> queryUserCoupons(Map<String, Object> map) {
        // 检查优惠券是否过期
        List<CouponDO> CouponDOs = apiCouponMapper.queryUserCoupons(map);
        for (CouponDO CouponDO : CouponDOs) {
            if (CouponDO.getCoupon_status() == 1) {
                // 检查是否过期
                if (CouponDO.getUse_end_date().before(new Date())) {
                    CouponDO.setCoupon_status(3);
                    apiCouponMapper.updateUserCoupon(CouponDO);
                }
            }
            if (CouponDO.getCoupon_status() == 3) {
                // 检查是否不过期
                if (CouponDO.getUse_end_date().after(new Date())) {
                    CouponDO.setCoupon_status(1);
                    apiCouponMapper.updateUserCoupon(CouponDO);
                }
            }
        }

        return CouponDOs;
    }

    public CouponDO queryMaxUserEnableCoupon(Map<String, Object> map) {
        return apiCouponMapper.queryMaxUserEnableCoupon(map);
    }

    public List<CouponDO> queryUserCouponList(Map<String, Object> map) {
        return apiCouponMapper.queryUserCouponList(map);
    }
}
